<?php

namespace service;
use think\Request;
//use http\Client\Curl;

class RequestService
{

    /**
     * @return string
     * @author: zhongyuan
     * @name: getRootUrl
     * @describe:获取网站的根Url
     */
   public static function getRootUrl()
    {
        if (BASE_PATH != '') {
            return BASE_PATH . '/';
        }
        return BASE_PATH;
    }



    /**
     * @param $path
     * @return bool|mixed|string
     * @author: zhongyuan
     * @name: root_full_path
     * @describe:渲染链接
     */
   public static function root_full_path($path)
    {
        //不存在http://
        $not_http_remote = (strpos($path, 'http://') === false);
        //不存在https://
        $not_https_remote = (strpos($path, 'https://') === false);
        if (substr($path,0,1)=='.') {
            $path= substr($path, 1);
        }
        if ($not_http_remote && $not_https_remote) {
            //本地url
            return str_replace('//', '/', getRootUrl() . $path); //防止双斜杠的出现
        }
        return $path;
    }

    /**
     * @param string $path
     * @return bool|string
     * @author: zhongyuan
     * @name: path_to_url
     * @describe:路径转换为url
     */
   public static function path_to_url($path ='')
    {
        if($path=='' || !$path) return false;

        if (strpos($path, 'http://')!==false || strpos($path, 'https://')!==false) return $path;//包含http和https的返回原值

        $url = get_uploadpath_url().$path;
        return $url;
    }

    /**
     * @param $url
     * @return string
     * @author: zhongyuan
     * @name: clear_urlcan
     * @describe:去除URL的参数
     */
   public static function clear_urlcan($url){
        $rstr   ='';
        $tmparr =parse_url($url);
        $rstr   =empty($tmparr['scheme'])?'http://':$tmparr['scheme'].'://';
        $rstr   .=$tmparr['host'].$tmparr['path'];
        return $rstr;
    }


    /**
     * @param null $ip
     * @return bool|mixed
     * @author: zhongyuan
     * @name: get_ip_lookup
     * @describe: 获取ip地址所在的区域
     */
   public static function get_ip_lookup($ip = null)
    {
        if (empty($ip)) {
            $ip = get_client_ip(0);
        }
        $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
        if (empty($res)) {
            return false;
        }
        $jsonMatches = array();
        preg_match('#\{.+?\}#', $res, $jsonMatches);
        if (!isset($jsonMatches[0])) {
            return false;
        }
        $json = json_decode($jsonMatches[0], true);
        if (isset($json['ret']) && $json['ret'] == 1) {
            $json['ip'] = $ip;
            unset($json['ret']);
        } else {
            return false;
        }
        return $json;
    }



    /**
     * @param int $type  返回类型 0 返回IP地址 1 返回IPV4地址数字
     * @param bool $adv  是否进行高级模式获取（有可能被伪装）
     * @return mixed
     * @author: zhongyuan
     * @name: get_client_ip
     * @describe:获取客户端IP地址
     */
     public static function get_client_ip($type = 0, $adv = false) {
        $type      = $type ? 1 : 0;
        static $ip = NULL;
        if ($ip !== NULL) {
            return $ip[$type];
        }

        if ($adv) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                $pos = array_search('unknown', $arr);
                if (false !== $pos) {
                    unset($arr[$pos]);
                }

                $ip = trim($arr[0]);
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u", ip2long($ip));
        $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
    }

    /**
     * @param $url
     * @return mixed
     * @author: zhongyuan
     * @name: get_content_by_url
     * @describe:todo 扩展多种采集方式，以备一种方式采集不到页面
     */
   public static function get_content_by_url($url){
        $md5 = md5($url);
        $content = cache('file_content_'.$md5);
        if(is_bool($content)){
            $content = self::curl_file_get_contents($url);
            cache('file_content_'.$md5,$content,60*60);
        }
        return $content;
    }


    /**
     * @param $durl
     * @return bool|string
     * @author: zhongyuan
     * @name: curl_file_get_contents
     * @describe:curl获取地址内容
     */
   public static function curl_file_get_contents($durl){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $durl);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_USERAGENT, '');
        curl_setopt($ch, CURLOPT_REFERER,'b');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        return $file_contents;
    }

    /**
     * @param $url
     * @return false|string
     * @author: zhongyuan
     * @name: wp_file_get_contents
     * @describe:防超时的file_get_contents改造函数
     */
   public static function wp_file_get_contents($url) {
        $context = stream_context_create ( array (
            'http' => array (
                'timeout' => 30
            )
        ) ); // 超时时间，单位为秒

        return file_get_contents ( $url, 0, $context );
    }

    /**
     * @param $uri
     * @param string $user
     * @param string $pw
     * @return mixed|string
     * @author: zhongyuan
     * @name: curl_remote_filesize
     * @describe:获取远程文件头信息
     */
    public static function curl_remote_filesize($uri,$user='',$pw='')
    {
        // start output buffering
        ob_start();
        // initialize curl with given uri
        $ch = curl_init($uri);
        // make sure we get the header
        curl_setopt($ch, CURLOPT_HEADER, 1);
        // make it a http HEAD request
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        // if auth is needed, do it here
        if (!empty($user) && !empty($pw))
        {
            $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        $okay = curl_exec($ch);
        curl_close($ch);
        // get the output buffer
        $head = ob_get_contents();
        // clean the output buffer and return to previous
        // buffer settings
        ob_end_clean();

        echo '<br>head-->'.$head.'<----end <br>';

        // gets you the numeric value from the Content-Length
        // field in the http header
        $regex = '/Content-Length:\s([0-9].+?)\s/';
        $count = preg_match($regex, $head, $matches);

        // if there was a Content-Length field, its value
        // will now be in $matches[1]
        if (isset($matches[1]))
        {
            $size = $matches[1];
        }
        else
        {
            $size = 'unknown';
        }
        //$last=round($size/(1024*1024),3);
        //return $last.' MB';
        return $size;
    }


    /**
     * @param $url
     * @param array|string $params
     * @param string $method
     * @param array $options
     * @return array
     * @author: zhongyuan
     * @name: curl_request
     * @describe: CURL发送Request请求,含POST和REQUEST
     */
    public static function curl_request($url, $params, $method = 'POST', $options = [], $userAgent = '' )
    {
        $method = strtoupper($method);
        $protocol = substr($url, 0, 5);
        $query_string = is_array($params) ? http_build_query($params) : $params;

        $ch = curl_init();
        $defaults = [];
        if ('GET' == $method)
        {
            $geturl = $query_string ? $url . (stripos($url, "?") !== FALSE ? "&" : "?") . $query_string : $url;
            $defaults[CURLOPT_URL] = $geturl;
        }
        else
        {
            $defaults[CURLOPT_URL] = $url;
            if ($method == 'POST')
            {
                $defaults[CURLOPT_POST] = 1;
            }
            else
            {
                $defaults[CURLOPT_CUSTOMREQUEST] = $method;
            }
            $defaults[CURLOPT_POSTFIELDS] = $query_string;
        }

        $defaults[CURLOPT_HEADER] = FALSE;
        $defaults[CURLOPT_USERAGENT] = empty($userAgent)?"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36":$userAgent;
        $defaults[CURLOPT_FOLLOWLOCATION] = TRUE;
        $defaults[CURLOPT_RETURNTRANSFER] = TRUE;
        $defaults[CURLOPT_CONNECTTIMEOUT] = 5;
        $defaults[CURLOPT_TIMEOUT] = 5;

        // disable 100-continue
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

        if ('https' == $protocol)
        {
            $defaults[CURLOPT_SSL_VERIFYPEER] = FALSE;
            $defaults[CURLOPT_SSL_VERIFYHOST] = FALSE;
        }

        curl_setopt_array($ch, (array) $options + $defaults);

        $result = curl_exec($ch);
        $err = curl_error($ch);

        $info = curl_getinfo($ch);

        if (FALSE === $result || !empty($err))
        {
            $errno = curl_errno($ch);
            curl_close($ch);
            return [
                'status'   => false,
                'errno' => $errno,
                'content'   => $err,
                'info'  => $info,
            ];
        }
        curl_close($ch);
        return [
            'status' => true,
            'content' => $result,
            'info'  => $info,
        ];
    }

    public static function isHttps($url)
    {
        $scheme = parse_url($url, PHP_URL_SCHEME);
        return (strtolower($scheme) == 'https');
    }

    /**
     * @param $url
     * @param $params
     * @return bool|string
     * @author: zhongyuan
     * @name: curl_post
     * @describe: curl_post 请求函数
     */
    public static function curl_post($url, $params, $header = [], $userAgent = '')
    {
        $query_string = is_array($params) ? http_build_query($params) : $params;

        $header[] = "Accept-Charset: utf-8";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        if (self::isHttps($url)) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        curl_setopt($ch, CURLOPT_USERAGENT, empty($userAgent) ? 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)' : $userAgent);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);//2017-09-21

        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        dump($response);exit;
        curl_close($ch);
        //$errorno = curl_errno($ch);
        return $response;
    }

    /**
     * @param $url
     * @return bool|string
     * @author: zhongyuan
     * @name: curl_get
     * @describe:get
     */
    public static function curl_get($url, $header = [], $userAgent = '' ){
        $ch = curl_init();
        //$header[] = "Accept-Charset: utf-8";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, true);//2017-09-21
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, empty($userAgent)?'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)':$userAgent);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $temp = curl_exec($ch);
        curl_close($ch);

        return $temp;
    }


    /**
     * @param $url
     * @param $param
     * @param bool $is_file
     * @param bool $return_array
     * @return bool|mixed|string
     * @author: zhongyuan
     * @name: post_data
     * @describe:以POST方式提交数据
     */
   public static function post_data($url, $param, $is_file = false, $return_array = true) {
        if (! $is_file && is_array ( $param )) {
            //$param = JSON ( $param );
            $param=json_encode($param);
        }
        if ($is_file) {
            $header [] = "content-type: multipart/form-data; charset=UTF-8";
        } else {
            $header [] = "content-type: application/json; charset=UTF-8";
        }

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)' );
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $param );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        $res = curl_exec ( $ch );
        $flat = curl_errno ( $ch );
        if ($flat) {
            $data = curl_error ( $ch );
            //addWeixinLog ( $flat, 'post_data flat' );
            //addWeixinLog ( $data, 'post_data msg' );
        }
        curl_close ( $ch );
        $return_array && $res = json_decode ( $res, true );
        return $res;
    }



    /**
     * @return bool
     * @author: zhongyuan
     * @name: is_weixin
     * @describe:是否微信访问
     */
   public static function is_weixin() {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
            return true;
        } else {
            return false;
        }
    }

    public static function download($url, $savepath, $timeout = 0, $user_agent = '', $refferer = '', $return = false)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $out = null;
        if(!empty($savepath)) {
            $dir = dirname($savepath);
            if (!is_dir($dir)) {
                mkdir($dir, 0766, true);
            }
            $out = fopen($savepath, "wb");
            if (!empty($out)){
                curl_setopt($ch, CURLOPT_FILE, $out);
            }
        }

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_USERAGENT, empty($user_agent) ? 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 ' : $user_agent);

        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        if(!empty($refferer)) {
            curl_setopt($ch, CURLOPT_REFERER, $refferer);
        }

        if ($timeout > 0) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        }

        $content = curl_exec($ch);

        $info = null;
        if ($return) {
            $info = curl_getinfo($ch);
        }
        curl_close($ch);

        if(empty($savepath) || empty($out)) {
            return $content;
        }else {
            fclose($out);
            chmod($savepath, 0766);
        }

        if ($return) {
            return $info;
        }

        return '';
    }


}